package com.yiqi.liebang.common.rongim.msg.provider;

import android.content.Context;
import android.content.Intent;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.suozhang.framework.framework.AM;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.rongim.msg.QuestionMessage;
import com.yiqi.liebang.common.widget.RatingBar;
import com.yiqi.liebang.feature.home.view.AskDetailActivity;
import com.yiqi.liebang.feature.home.view.TopicInfoActivity;

import butterknife.BindView;
import io.rong.imkit.model.ProviderTag;
import io.rong.imkit.model.UIMessage;
import io.rong.imkit.widget.provider.IContainerItemProvider;
import io.rong.imlib.model.Message;

@ProviderTag(messageContent = QuestionMessage.class)
public class QuestionMessageItemProvider extends IContainerItemProvider.MessageProvider<QuestionMessage> {

    class ViewHolder {

        TextView mTvUserQuestionContent;
        LinearLayout linearLayout;
        TextView mTvType;
    }

    @Override
    public View newView(Context context, ViewGroup group) {
        View view = LayoutInflater.from(context).inflate(R.layout.share_question, null);
        ViewHolder holder = new ViewHolder();
        holder.mTvUserQuestionContent = view.findViewById(R.id.tv_user_question_content);
        holder.linearLayout=view.findViewById(R.id.bg_ques);
        holder.mTvType=view.findViewById(R.id.tv_user_question_type);
        view.setTag(holder);
        return view;
    }


    @Override
    public void bindView(View v, int i, QuestionMessage item, UIMessage uiMessage) {
        ViewHolder holder = (ViewHolder) v.getTag();
        if (uiMessage.getMessageDirection() == Message.MessageDirection.SEND) {
            holder.linearLayout.setBackgroundResource(io.rong.imkit.R.drawable.rc_ic_bubble_right);
            holder.mTvType.setTextColor(AM.res().getColor(R.color.text_999999));
        } else {
            holder.linearLayout.setBackgroundResource(io.rong.imkit.R.drawable.rc_ic_bubble_left);
            holder.mTvType.setTextColor(AM.res().getColor(R.color.text_secondary));

        }
        if (item != null) {
            holder.mTvUserQuestionContent.setText(item.getContent());
            holder.mTvType.setText(TextUtils.isEmpty(item.getDetailType())?"":item.getDetailType().equals("1")?"猎帮问答分享":"猎帮话题分享");
        }
    }

    @Override
    public Spannable getContentSummary(QuestionMessage data) {
        return new SpannableString(data.getDetailType().equals("1")?"[猎帮问答分享]":"[猎帮话题分享]");
    }

    @Override
    public void onItemClick(View view, int i, QuestionMessage content, UIMessage uiMessage) {
        if (content != null) {
           if (content.getDetailType().equals("1")){
               Intent intent = new Intent(AM.app(), AskDetailActivity.class);
               intent.putExtra("answer_id", content.getDetailUid());
               intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               AM.app().startActivity(intent);
           }else  if (content.getDetailType().equals("2")){
               Intent intent = new Intent(AM.app(), TopicInfoActivity.class);
               intent.putExtra("topic", content.getDetailUid());
               intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
               AM.app().startActivity(intent);
           }
        }
    }


}